package ocp.context;

import java.util.Vector;
import java.util.Enumeration;

/**
 * ContextualInformationList. Representation of a list of contextual information
 * @author Ignacio Nieto Carvajal.
 */
public class ContextualInformationList implements java.io.Serializable 
{
	/** The vector of contextualInformations */
	protected Vector<ContextualInformation> contextualInformations;

	/** 
	 * The main constructor of the class. It creates a new void contextualInformation list
	 */
	public ContextualInformationList ()
	{
		contextualInformations = new Vector();
	}

	/**
	 * An alternate constructor of the class, takes an array of contextualInformations as
	 * argument.
	 * @param contInfattrgcs the list of contextualInformations to include in this 
	 * ContextualInformationList.
	 */
	public ContextualInformationList (ContextualInformation contInfattrgcs[])
	{
		this();
		for (int i = 0; i < contInfattrgcs.length; i++)
			contextualInformations.addElement(contInfattrgcs[i]);
	}

	/** Adds an contextualInformation to the contextualInformation list. It the contextualInformation is already in
	 * the contextualInformation list (even with another value) old one is overwriten */
	public synchronized void addContextualInformation (ContextualInformation contInfattrgc)
	{
		ContextualInformation actual;
		
		for (int i = 0; i < contextualInformations.size(); i++)
		{
			actual =  contextualInformations.elementAt(i);
			if (actual.equals(contInfattrgc))
			{
				contextualInformations.removeElementAt(i);
                                contextualInformations.addElement(contInfattrgc);
				return;
			}
		}

		// if there were no match, insert the contextualInformation.
		contextualInformations.addElement(contInfattrgc);
	}
        
	/** Adds several contextualInformations to this contextualInformation list. 
         * If any of the contextualInformations contained in the list is already in
	 * this contextualInformation list (even with another value) old one is overwriten */
	public synchronized void addContextualInformationList (ContextualInformationList cil)
	{
            for (Enumeration<ContextualInformation> e = cil.getContextualInformation(); e.hasMoreElements();) {
                this.addContextualInformation( e.nextElement());
            }
	}

	/** 
	 * removes an contextualInformation from the contextualInformation list 
	 * @param contInfattrgc the exact ContextualInformation to remove
	 * @return true if the contextualInformation was in the contextualInformation list. False otherwise.
	 */
	public synchronized boolean removeContextualInformation(ContextualInformation contInfattrgc)
	{
		return contextualInformations.removeElement(contInfattrgc);
	}

        /** 
	 * removes all contextualInformations given in the parameter cil 
         * from this contextualInformation list.
	 * @param cil list of contextual informations to remove.
	 * @return an integer showing the total number of contextual informations deleted.
	 */
	public synchronized int removeContextualInformationList(ContextualInformationList cil)
	{
            int total = 0;
            for (Enumeration<ContextualInformation> e = cil.getContextualInformation(); e.hasMoreElements();)
                if (this.removeContextualInformation( e.nextElement()))
                    total++;
            return total;
	}

	/** 
	 * removes an contextualInformation from the contextualInformation list 
	 * @param contInfattrgcName the string containing the name of the contextualInformation to remove
	 * @return true if the contextualInformation was in the contextualInformation list. False otherwise.
	 */
	public boolean removeContextualInformation(String contInfattrgcName)
	{
		ContextualInformation actual;
		
		for (int i = 0; i < contextualInformations.size(); i++)
		{
			actual =  contextualInformations.elementAt(i);
			if (actual.getName().equals(contInfattrgcName))
			{
				return contextualInformations.removeElement(actual);
			}
		}
		return false;
	}
        
        /**
         * removes a list of contextual information from this list based on their names.
         * @param ciNames names of the contextual informations to remove in array format.
         * @return the total number of contextual informations removed.
         */
        public synchronized int removeContextualInformationList(String ciNames []) {
            int total = 0;
            
            for (int i = 0; i < ciNames.length; i++)
                if (this.removeContextualInformation(ciNames[i]))
                    total++;
            return total;
        }
        
        public int getSize()    { return contextualInformations.size(); }        

	/** Clears all contextualInformations from this list. */
	public synchronized void removeAllContextualInformation()	{ contextualInformations.removeAllElements(); }
	

	/**
	 * modifies the value of an contextualInformation if found in the contextualInformation list.
	 * @return true if the contextualInformation was in the contextualInformation list. False otherwise.
	 */
	public synchronized boolean modifyContextualInformation(ContextualInformation contInf)
	{
		ContextualInformation actual = null;
		
		for (int i = 0; i < contextualInformations.size(); i++)
		{
			actual =  contextualInformations.elementAt(i);
			if (actual.equals(contInf))
			{
                                contextualInformations.removeElementAt(i);
                                contextualInformations.addElement(contInf);
				return true;
			}
		}
		return false;
	}

        /**
         * modifies the value of several contextual informations contained in a list.
         * @return the total number of contextual informations modified.
         */
        public synchronized int modifyContextualInformationList(ContextualInformationList cil) {
            int total = 0;
            
            for (Enumeration<ContextualInformation> e = cil.getContextualInformation(); e.hasMoreElements();)
                if (this.modifyContextualInformation( e.nextElement()))
                    total++;
            return total;
        }
        
	/**
	 * Returns an enumeration with the contextualInformations of the list.
	 */
	public synchronized Enumeration getContextualInformation()
	{
		return contextualInformations.elements();
	}

        /**
         * A method for retrieving a given contextual information of a list if it exists.
         * @param name the name of the contextual information to retrieve
         * @return the contextual information identified with "name" or null if it does not
         * exists in the list.
         */
        public synchronized ContextualInformation getContextualInformation(String name) {
            for (Enumeration<ContextualInformation> e = contextualInformations.elements(); e.hasMoreElements();) {
                ContextualInformation ci =  e.nextElement();
                if (ci.getName().equals(name)) return ci;
            }
            return null;
        }
        
	/** Returns true if the list is empty. false otherwise. */
	public boolean isEmpty()
	{
		return contextualInformations.isEmpty();
	}

        /**
         * Returns the contextual information at position i. i must be between
         * 0 and size - 1.
         */
        public ContextualInformation getContextualInformationAt(int i) {
            return contextualInformations.elementAt(i);
        }
}

